// ov是应用商店app的版本   nv其实指的是后台添加的版本
// 判断版本是否需要更新的方法，应用商店app的版本号和后台添加的版本号进行对比（一位数一位数的对比），后台添加版本号应该大于应用商店app的版本号
export function compareVersion(ver, newVer) { // ov为本地历史版本,nv为当前线上版本

	// let version = ver.split('.');
	// let newVersion = newVer.split('.');
	// if (parseInt(newVersion[0]) > parseInt(version[0])) {
	//     return true;
	// } else if (parseInt(newVersion[0]) == parseInt(version[0])) {
	//     if (parseInt(newVersion[1]) > parseInt(version[1])) {
	//         return true;
	//     } else if (parseInt(newVersion[1]) == parseInt(version[1])) {
	//         if (parseInt(newVersion[2]) > parseInt(version[2])) {
	//             return true;
	//         }
	//     }
	// }
	// return false;
	if (newVer > ver) {
		return true
	} else {
		return false
	}
}

import {
	getNewVersion
} from '@/apis/nurse/index.js'

import { httpUrl } from '@/utils/options.js'
//新版本
export function update() {
	return new Promise((resolve, rejcet) => {
		var isAndroid = uni.getSystemInfoSync().platform;

		var systemType = isAndroid == 'android' ? 0 : 1;
		uni.request({
			url: httpUrl + '/nurse/login/version/getNewVersion',
			// url: 'http://http://192.168.1.32:5002/nurse/login/version/getNewVersion',
			method: 'GET',
			data: {
				systemType: systemType
			},
			success: (res) => {
				console.log(res, 'res------------------------');
				if (res.data.object) {
					plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
						console.log(widgetInfo.versionCode, res.data.object.number,
							compareVersion(widgetInfo.versionCode, res.data.object
								.number), 'jieguo ...........');
						if (compareVersion(widgetInfo.versionCode, res.data.object
								.number)) {
							// 非强制更新
							resolve(res)

						} else {
							// 当前是最新版本
							resolve({
								data: {
									success: false
								}
							})
						}
					})
				} else {
					// 当前是最新版本
					resolve({
						data: {
							success: false
						}
					})
				}
			},


		});
	})
}

// 创建下载对象
export function downWgt(address) {
	var wgtWaiting = plus.nativeUI.showWaiting("开始下载");
	var wgtUrl = address;
	var wgtOption = {
		filename: "_doc/update/",
		retry: 1
	};
	// 新建下载任务
	var task = plus.downloader.createDownload(wgtUrl, wgtOption, function(download, status) {
		if (status == 200) {
			console.log('000000000000000')
			wgtWaiting.close();
			//安装wgt包文件
			installWgt(download.filename);
		} else {
			plus.nativeUI.toast('应用升级失败')
			// uni.showToast({
			// 	title:'应用升级失败',
			// 	icon:'none'
			// })
			wgtWaiting.close();
		}
	});
	task.addEventListener("statechanged", function(download, status) {
		console.log(download.state)
		switch (download.state) {
			case 2:
				wgtWaiting.setTitle("已连接到服务器");
				break;
			case 3:
				var percent = download.downloadedSize / download.totalSize * 100;
				wgtWaiting.setTitle("已下载 " + parseInt(percent) + "%");
				break;
			case 4:
				wgtWaiting.setTitle("下载完成");
				break;
		}
	});
	task.start();
}
// 安装wgt文件
export function installWgt(path) {
	plus.nativeUI.showWaiting("安装wgt文件...");
	plus.runtime.install(path, {}, function() {
		plus.nativeUI.closeWaiting();
		console.log("安装成功！");
		plus.nativeUI.alert("应用资源更新完成！", function() {
			plus.runtime.restart();
		});
	}, function(e) {
		console.log(e);
		plus.nativeUI.closeWaiting();
		//		console.log("安装wgt文件失败[" + e.code + "]：" + e.message);
		//		plus.nativeUI.alert("安装wgt文件失败[" + e.code + "]：" + e.message);
		mui.alert('请前往APP商城下载', '安装失败', '关闭', function(e) {
			if (e.index == 0) {

			}
		})
	});
}